package com.ds.infrastructure.hyperspace.console.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.Date;

/**
 * @author: yaozhendong
 * @create: 2019-11-04 17:19
 **/
@Slf4j
public class MetricsUtil {
    private MetricsUtil() {

    }

    private static String executeTimePattern = "yyyyMMddHHmm";

    public static long getPreOneMinuteExecuteTime(Long executeTime) {
        try {
            Date dt = DateUtils.parseDate(executeTime.toString(), executeTimePattern);
            return dateToTaskExecuteTime(DateUtils.addMinutes(dt, -1));
        } catch (ParseException e) {
            log.error("", e);
            throw new IllegalStateException(e);
        }
    }


    public static long dateToTaskExecuteTime(Date taskExecuteTime) {
        return Long.parseLong(DateFormatUtils.format(taskExecuteTime, executeTimePattern));
    }

    /**
     * 当前日期最小值
     *
     * @param dt
     * @return
     */
    public static Date getDateMinValue(Date dt) {
        String pattern = "yyyyMMdd";
        try {
            return DateUtils.parseDate(DateFormatUtils.format(dt, pattern), pattern);
        } catch (ParseException e) {
            log.error("", e);
            throw new IllegalStateException(e);
        }
    }
}
